package p3as.cliente;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Scene implements Drawable {

    private ArrayList<Drawable> sceneList = new ArrayList();
    private Semaphore mutex = new Semaphore(1);
    
    @Override
    public void addChild(Drawable drawable) {
        try {
            mutex.acquire();
            sceneList.add(drawable);
            mutex.release();
        } catch (InterruptedException ex) {
            Logger.getLogger(Scene.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void removeChild(Drawable drawable) {
        try {
            mutex.acquire();
            sceneList.remove(drawable);
            mutex.release();
        } catch (InterruptedException ex) {
            Logger.getLogger(Scene.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public Drawable getChild(int index) { 
        return sceneList.get(index);
    }

    @Override
    public int collision(Drawable drawable) {
        try {
            mutex.acquire();
            for(Drawable d : sceneList){
                d.collision(drawable);
            }
            mutex.release();
            
        } catch (InterruptedException ex) {
            Logger.getLogger(Scene.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    @Override
    public void render(Graphics graphics) {
        try {
            mutex.acquire();
            for(Drawable d : sceneList) d.render(graphics);
            mutex.release();
        } catch (InterruptedException ex) {
            Logger.getLogger(Scene.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getSize(){
        return sceneList.size();
    }
    
    @Override
    public int getX() {
        return -1;
    }

    @Override
    public int getY() {
        return -1;
    }

    @Override
    public int getWidth() {
        return -1;
    }

    @Override
    public int getHeigth() {
        return -1;
    }

    @Override
    public void setScore(Score score) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
